$(document).ready(function () {
    $('#logout').find("span").text("（" + localStorage.getItem("real_name") + "）");
    var $nav = $("#admin-nav");
    var rel = $nav.data("index");
    if (rel) {
        $nav.find("a[rel=" + rel + "]").addClass("active");
    }
});

function ajaxForm(selector, onSuccess) {
    var $form = $(selector);
    var $submit = $form.find(".submit");
    var errorFunc = function () {
        var $err = $('#form-result-message').find(".error");
        $err.slideDown(100);
        $err.find("span").text("系统错误");
        if (window.$timeout) {
            clearTimeout(window.$timeout);
        }
        window.$timeout = setTimeout(function () {
            $submit.fadeIn(100);
            $err.slideUp(100);
        }, 2000);
    };
    var loadingFunc = function () {
        $submit.fadeOut(100);
    };
    var successFunc = function (json) {
        if (!json.res) {
            errorFunc();
            $('#form-result-message').find(".error span").text(json.msg);
            return;
        }
        var $success = $('#form-result-message').find(".success");
        $success.slideDown(100);
        if (window.$timeout) {
            clearTimeout(window.$timeout);
        }
        window.$timeout = setTimeout(function () {
            $submit.fadeIn(100);
            $success.slideUp(100);
        }, 2000);
    };
    $form.ajaxForm({
        dataType: "json",
        beforeSubmit: loadingFunc,
        error: errorFunc,
        success: function (json) {
            successFunc(json);
            if (onSuccess) {
                onSuccess(json);
            }
        }
    })
}

function ajaxRequest(selector, url, method, data, onSuccess) {
    var $form = $(selector);
    var $submit = $form.find(".submit");
    var errorFunc = function () {
        var $err = $('#form-result-message').find(".error");
        $err.slideDown(100);
        $err.find("span").text("系统错误");
        if (window.$timeout) {
            clearTimeout(window.$timeout);
        }
        window.$timeout = setTimeout(function () {
            $submit.fadeIn(100);
            $err.slideUp(100);
        }, 2000);
    };
    var loadingFunc = function () {
        $submit.fadeOut(100);
    };
    var successFunc = function (json) {
        if (!json.res) {
            errorFunc();
            $('#form-result-message').find(".error span").text(json.msg);
            return;
        }
        var $success = $('#form-result-message').find(".success");
        $success.slideDown(100);
        if (window.$timeout) {
            clearTimeout(window.$timeout);
        }
        window.$timeout = setTimeout(function () {
            $submit.fadeIn(100);
            $success.slideUp(100);
        }, 2000);
    };
    $.ajax({
        url: url,
        method: method,
        data: data,
        dataType: "json",
        beforeSend: loadingFunc,
        error: errorFunc,
        success: function (json) {
            successFunc(json);
            if (onSuccess) {
                onSuccess(json);
            }
        }
    });
}

function commentInit() {
    var $status_list = $("#comment-status-list");
    var status = $status_list.data("status");
    $status_list.find("a[rel=" + status + "]").parent().addClass("active");
    if (status == "approved") {
        $('.comment-approve').remove();
    }
    if (status == "spam") {
        $('.comment-spam').remove();
    }
    var $author = $(".comment-author");
    if ($author.attr("href") != "#") {
        $author.attr("target", "_blank");
    }
}

function commentAction() {
    var $form = $('#comment-reply-form');
    var $parent = $('#comment-reply-parent');
    $(".comment-reply").on("click", function () {
        var $this = $(this);
        var id = $this.attr("href").toString().replace("#comment-", "");
        if ($this.hasClass("replying")) {
            $parent.val(0);
            $this.removeClass("replying").text("回复");
            $form.hide();
            return false;
        }
        $(".replying").trigger("click");
        $("#comment-" + id).append($form.detach().show());
        $parent.val(id);
        $this.addClass("replying").text("取消回复");
        return false;
    });
    ajaxForm("#comment-reply-form", function (json) {
        if (json.res) {
            var $comment = $("#comment-" + json.comment.parent_id);
            $comment.append('<div class="comment-reply-comment">' + json.comment.content + '</div>');
            $(".replying").trigger("click");
        } else {
            alert("回复出错");
        }
    });
    $('.comment-remove').on("click", function () {
        if (!confirm("确认删除？")) {
            return false;
        }
        var id = $(this).attr("href").toString().replace("#comment-", "");
        ajaxRequest(null, "/admin/comments/" + id + "/", "delete", {}, function (json) {
            if (json.res) {
                $('#comment-' + id).remove();
            }
        });
        return false;
    });
    $('.comment-approve').on("click",function(){
        var id = $(this).attr("href").toString().replace("#comment-", "");
        ajaxRequest(null,"/admin/comments/status","post",{"id":id,"status":"approved"},function(json){
             if(json.res){
                 $('#comment-' + id).remove();
             }
        });
        return false;
    });
    $('.comment-spam').on("click",function(){
        var id = $(this).attr("href").toString().replace("#comment-", "");
        ajaxRequest(null,"/admin/comments/status","post",{"id":id,"status":"spam"},function(json){
            if(json.res){
                $('#comment-' + id).remove();
            }
        });
        return false;
    });
}
